<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ReGoodsController extends Controller
{
    /**
     * 退换货列表
     */
    public function getIndex(Request $request)
    {
        // $reGoods = DB::table('reGoods')
        // ->leftJoin('users','reGoods.uid','=','users.id')
        // ->leftJoin('goods','reGoods.gid','=','goods.id')
        // ->leftJoin('useraddress','reGoods.uid','=','useraddress.userid')

        // ->select('reGoods.*','users.username','goods.proname','useraddress.phone')

        // ->where(function($query)use($request){
        //     $query->where('users.username','like','%'.$request->input('keywords').'%');
        // })
        // ->paginate($request->input('num',10));

        $reGoods = DB::table('reGoods')
        ->leftJoin('orders','reGoods.order_id','=','orders.id')
        ->leftJoin('users','reGoods.uid','=','users.id')
        ->leftJoin('useraddress','orders.address_id','=','useraddress.id')
        ->leftJoin('order_goods','order_goods.order_id','=','orders.id')
        ->select('reGoods.*','orders.order_code','orders.status','orders.address_id','useraddress.phone','order_goods.goods_name','users.username')
        ->whereIn('orders.status',[3,4])
        ->where(function($query)use($request){
            $query->where('order_code','like','%'.$request->input('keywords').'%');
        })
        ->paginate($request->input('num',10));
        // ->get();
        // dd($reGoods);


        //解析模板
        return view('reGoods.index',['reGoods'=>$reGoods,'request'=>$request->all()]);
    }

    /**
     * 修改
     */
    public function getEdit($id)
    {
        //查询数据
        $res = DB::table('reGoods')
        ->leftJoin('orders','reGoods.order_id','=','orders.id')
        ->leftJoin('users','reGoods.uid','=','users.id')
        ->leftJoin('useraddress','orders.address_id','=','useraddress.id')
        ->leftJoin('order_goods','order_goods.order_id','=','orders.id')
        ->select('reGoods.*','orders.order_code','orders.status','orders.address_id','useraddress.phone','order_goods.goods_name','users.username')

        ->where('reGoods.id',$id)
        ->first();
        //显示模板 分配变量
        return view('reGoods.edit',['reGoodsinfo'=>$res]);
    }

    public function postUpdate(Request $request)
    {
        //接受数据
        $date = $request->only(['statuss']);
        $id = $request->input('id');
        //执行修改
        $res1 = DB::table('reGoods')->where('id',$id)->update($date);
        if($res1){
            //成功
            return redirect('/admin/regoods/index')->with('success','数据修改成功');
        }else{
            return back()->with('error','数据修改失败');
        }

    }

   

}
